package mercurius.fitness.client.ui;

import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import mercurius.fitness.client.Fitness;
import mercurius.fitness.client.util.Entry;
import mercurius.fitness.client.util.HTTPUtil;
import net.pleso.framework.client.ui.windows.Slider;
import net.pleso.framework.client.ui.windows.Window;


public class IFrame extends Window  implements WindowResizeListener {

	private Frame frame = new Frame();
	private VerticalPanel v = new VerticalPanel();
	private HTML link = new HTML("<a href='javascript:;'>Open in new window</a>");
	private String url = "";
	
	public IFrame(String caption,String url) {
		super(null);
		init(caption,url);
	}
	
	public IFrame(String caption,Entry[] entries) {
		super(null);
		init(caption,Fitness.get().getURL()+"?"+HTTPUtil.buildQueryString(entries));
		
	}
	
	private void init(String caption,final String url) {
		this.url = url;
		this.setCaption(caption);
		SimplePanel p = new SimplePanel();
		link.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				Fitness.open(url);
			}
		});
		
		v.add(link);
		v.add(frame);
		v.setWidth("100%");
		p.add(v);
		this.initWidget(p);
		
		int height = com.google.gwt.user.client.Window.getClientHeight() - 75;
		int width = com.google.gwt.user.client.Window.getClientWidth();
		
		onWindowResized(width,height);
		
		// Add a listener for browser resize events.
			com.google.gwt.user.client.Window.addWindowResizeListener(this); 

	}
	
	public void onWindowResized(int width, int height) {
	 height = com.google.gwt.user.client.Window.getClientHeight();
	 width = com.google.gwt.user.client.Window.getClientWidth();
	
	 width = width - 10;
	 height = height - 110;
	 
     frame.setHeight(height + "px");
     frame.setSize("100%",height + "px"); 
         
       v.clear();
       v.add(link);
       v.add(new HTML("<hr/>"));
       v.setHeight("100%");
       v.setWidth("100%");
       v.add(frame);
       frame.setUrl(url);
	}
	
	public IFrame(Slider parentSlider,String caption,String url) {
		super(parentSlider);
		init(caption,url);
	}
}
